<?php
class carModel extends appModel
{
    //获取奖品信息
    
    
    public function get_prize($prize_name,$support,$page, $limit = 10)
    {
        $listPage=array('data'=>'','pageLink'=>'');
        
        $where = ' WHERE 1=1 ';
        
        if(isset($prize_name) && $prize_name!='')
        {
            $where.=' and prize_name like "%'.$prize_name.'%"';
        }
        
        if(isset($support) && $support!='')
        {
            $where.=' and support like "%'.$support.'%"';
        }
        
        $sql = 'SELECT * FROM '.$this->table('chezhan_prize').$where." ORDER BY card_type desc , pri desc ";
        $result=$this->show_page($sql, $page, $limit);
        $listPage['pagelink']=$result['page'];
        $listPage['data']=$result['result'];
        return $listPage;
    }
    
    public function get_prize_one($id)
    {
       
        $sql = 'SELECT * FROM '.$this->table('chezhan_prize').' WHERE id = "'.$id.'"';
        $res = $this->getRow($sql);
//         var_dump($res);
        return $res;
    }
    
    public function get_rule_one($id)
    {
         
        $sql = 'SELECT a.*,b.prize_name FROM '.$this->table('chezhan_prize_period').' as a LEFT JOIN '.$this->table('chezhan_prize').' as b ON a.pid=b.id WHERE a.id="'.$id.'"';
        $res = $this->getRow($sql);
        
        $res['begintime'] = date('Y-m-d H:i:s',$res['begintime']);
        $res['endtime'] = date('Y-m-d H:i:s',$res['endtime']);
        
        return $res;
    }
    
    //获取奖品时段信息
    public function get_rule($prize_name,$id,$page, $limit = 10)
    {
        $listPage=array('data'=>'','pageLink'=>'');
        
        $where = ' WHERE 1=1 ';
        
        if(isset($prize_name) && $prize_name!='')
        {
            $where.=' and b.prize_name like "%'.$prize_name.'%"';
        }
        
        if(isset($id) && $id!='')
        {
            $where.=' and b.id = "'.$id.'"';
        }
        
        $sql = 'SELECT a.*,b.prize_name,b.card_type,b.pri FROM '.$this->table('chezhan_prize_period').' as a LEFT JOIN '.$this->table('chezhan_prize').' as b ON a.pid=b.id'.$where." ORDER BY b.card_type desc , b.pri desc  ";
        $result=$this->show_page($sql, $page, $limit);
        $listPage['pagelink']=$result['page'];
        $listPage['data']=$result['result'];
        return $listPage;
    }
    //获取中奖用户信息
    public function get_win_users($prize_name,$page, $limit = 10)
    {
        $listPage=array('data'=>'','pageLink'=>'');
    
        $where = ' WHERE 1=1 ';
    
        if(isset($prize_name) && $prize_name!='')
        {
            $where.=' and b.prize_name like "%'.$prize_name.'%"';
        }
    
//         ,b.prize_name,c.nickname
        $sql = 'SELECT a.addtime,a.is_card,b.prize_name,c.nickname FROM '.$this->table('chezhan_prize_user').' as a '.
                'LEFT JOIN '.$this->table('wxusers').' as c ON a.wxid=c.wxid '.
                'LEFT JOIN '.$this->table('chezhan_prize').' as b ON a.pid=b.id '.
                $where." ORDER BY  addtime desc ";
        $result=$this->show_page($sql, $page, $limit);
        $listPage['pagelink']=$result['page'];
        $listPage['data']=$result['result'];
        return $listPage;
    }
    
    
    public function get_users($prize_name,$page, $limit = 10)
    {
        $listPage=array('data'=>'','pageLink'=>'');
    
        $where = ' WHERE 1=1 ORDER BY addtime desc  ';
    
        if(isset($prize_name) && $prize_name!='')
        {
            $where.=' and b.user_name like "%'.$prize_name.'%"';
        }
    
    
        $sql = 'SELECT a.*,b.openid , b.nickname FROM '.$this->table('info_record').' as a LEFT JOIN '.$this->table('wxusers').' as b ON a.wxid=b.wxid'.$where;
        $result=$this->show_page($sql, $page, $limit);
        $listPage['pagelink']=$result['page'];
        $listPage['data']=$result['result'];
        $listPage['count']=$result['count'];
        return $listPage;
    }
    
    
    public function listAll()
    {
        $sql = ' SELECT a.id , b.openid , b.nickname,a.username,a.phone,a.addtime FROM '.$this->table('info_record').' as a LEFT JOIN '.$this->table('wxusers').' as b ON a.wxid=b.wxid  WHERE  a.phone !=""  ';
        
        $result = $this->getAll($sql);
        
        $i = 0;
        
        foreach ($result as $k => $v)
        {
            $i++;
            $result[$k]['id'] = $i;
            $result[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
        }
        
        
        return $result;
    }
    
    //保存奖品信息
    public function set_prize($id,$prize_name,$support,$money,$mark,$pby,$pri,$card_id,$card_type=0)
    {
        if(!empty($id)){
            $sql = "UPDATE ".$this->table('chezhan_prize')." SET prize_name='$prize_name',card_type='$card_type',support='$support',money='$money',mark='$mark',pby='$pby',pri='$pri',card_id='$card_id' WHERE id='".$id."'";
            $res = $this->query($sql);
        }else{
            $sql = "INSERT INTO ".$this->table('chezhan_prize')." (card_type,prize_name,support,money,mark,pby,pri,card_id) values ('".$card_type."','".$prize_name."','".$support."','".$money."','".$mark."','".$pby."','".$pri."','".$card_id."')";
	        $res = $this->query($sql);
        }
        return $res;
        
    }
    //删除指定记录
    public function del_prize($id)
    {
        $result=array('error'=>0,'content'=>'删除成功');
        if($id)
        {
        
            $sql = "DELETE FROM ".$this->table('chezhan_prize')." WHERE id='".$id."'";
        
            $res=$this->query($sql);
        
            if(!$res)
            {
                $result['error']=1;
                $result['content']='删除失败';
            }
        
        
        }
        return $result;
        
    }
    
    //删除指定记录
    public function del_rule($id)
    {
        $result=array('error'=>0,'content'=>'删除成功');
        if($id)
        {
        
            $sql = "DELETE FROM ".$this->table('chezhan_prize_period')." WHERE id='".$id."'";
        
            $res=$this->query($sql);
        
            if(!$res)
            {
                $result['error']=1;
                $result['content']='删除失败';
            }
        
        
        }
        return $result;
        
    }
    
    //获取所有奖品名称
    public function get_prize_name()
    {
        $sql = 'SELECT prize_name,id FROM '.$this->table('chezhan_prize');
        return $this->getAll($sql);
    }
    //保存奖品时段信息
    public function set_rule($id,$pid,$nums,$begintime,$endtime)
    {
        if(!empty($id)){
            $sql = "UPDATE ".$this->table('chezhan_prize_period')." SET pid='$pid',nums='$nums',begintime='$begintime',endtime='$endtime' WHERE id='".$id."'";
            $res = $this->query($sql);
        }else{
            $sql = "INSERT INTO ".$this->table('chezhan_prize_period')." (pid,nums,begintime,endtime) values ('".$pid."','".$nums."','".$begintime."','".$endtime."')";
            $res = $this->query($sql);
        }
        return $res;
    
    }
    
    
}?>